using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace LANLauncher
{
	public class Setting
	{
		public string Name;

		public string Value;

		public string WorkingValue;

		public string DefaultValue;

		public Dictionary<string, string> PossibleValues;

		public Control AttachedControl;

		public Label AttachedLabel;

		public bool IsValid;

		public Setting(string _id, string _value, Control _comboBox, Label _label)
		{
			this.Name = _id;
			this.Value = _value;
			this.WorkingValue = _value;
			this.AttachedControl = _comboBox;
			this.AttachedLabel = _label;
			this.IsValid = true;
			if (this.AttachedControl != null)
			{
				if (this.AttachedControl is ComboBox)
				{
					ComboBox attachedControl = (ComboBox)this.AttachedControl;
					attachedControl.SelectedIndexChanged += new EventHandler(this.AttachedControl_SelectedIndexChanged);
					attachedControl.SelectedIndex = -1;
				}
				else if (this.AttachedControl is TextBox)
				{
					((TextBox)this.AttachedControl).TextChanged += new EventHandler(this.tb_TextChanged);
				}
				else if (!(this.AttachedControl is TrackBar))
				{
					MessageBox.Show("");
				}
				else
				{
					TrackBar trackBar = (TrackBar)this.AttachedControl;
					float single = 1f;
					float.TryParse(_value, out single);
					trackBar.Value = (int)(single * 10f);
					trackBar.ValueChanged += new EventHandler(this.trb_ValueChanged);
				}
			}
			this.PossibleValues = new Dictionary<string, string>();
		}

		public bool AddValue(string _value)
		{
			return this.AddValue(_value, StringLocaliser.LocaliseString(_value, true));
		}

		public bool AddValue(string _value, string _localisedValue)
		{
			if (this.AttachedControl is ComboBox)
			{
				ComboBox attachedControl = (ComboBox)this.AttachedControl;
				int num = attachedControl.Items.IndexOf(_localisedValue);
				if (num < 0)
				{
					num = attachedControl.Items.Add(_localisedValue);
					this.PossibleValues.Add(_value, _localisedValue);
				}
				if (_value == this.Value)
				{
					attachedControl.SelectedIndex = num;
					return true;
				}
			}
			else if (this.AttachedControl is TrackBar)
			{
				TrackBar trackBar = (TrackBar)this.AttachedControl;
				float single = 1f;
				float.TryParse(_value, out single);
				trackBar.Value = (int)(single * 10f);
			}
			return false;
		}

		private void AttachedControl_SelectedIndexChanged(object sender, EventArgs e)
		{
			ComboBox comboBox = (ComboBox)sender;
			if (comboBox.SelectedItem == null)
			{
				this.Value = "";
				if (this.AttachedLabel != null)
				{
					this.AttachedLabel.ForeColor = Color.Red;
				}
				this.IsValid = false;
				return;
			}
			foreach (KeyValuePair<string, string> possibleValue in this.PossibleValues)
			{
				if (comboBox.SelectedItem.ToString() != possibleValue.Value)
				{
					continue;
				}
				this.WorkingValue = possibleValue.Key;
				break;
			}
			if (this.AttachedLabel != null)
			{
				this.AttachedLabel.ForeColor = Color.White;
			}
			this.IsValid = true;
		}

		public void Clear()
		{
			this.PossibleValues.Clear();
			if (this.AttachedControl is ComboBox)
			{
				((ComboBox)this.AttachedControl).Items.Clear();
			}
		}

		public string DelocaliseValue(string _value)
		{
			string key;
			Dictionary<string, string>.Enumerator enumerator = this.PossibleValues.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					KeyValuePair<string, string> current = enumerator.Current;
					if (_value != current.Value)
					{
						continue;
					}
					key = current.Key;
					return key;
				}
				return "unknown";
			}
			finally
			{
				((IDisposable)enumerator).Dispose();
			}
			//return key;
		}

		public void LocaliseValues()
		{
			if (!(this.AttachedControl is ComboBox))
			{
				if (this.AttachedControl is TrackBar)
				{
					TrackBar attachedControl = (TrackBar)this.AttachedControl;
					float single = 1f;
					float.TryParse(this.WorkingValue, out single);
					attachedControl.Value = (int)(single * 10f);
				}
				return;
			}
			ComboBox comboBox = (ComboBox)this.AttachedControl;
			comboBox.Items.Clear();
			int num = 0;
			Dictionary<string, string> strs = new Dictionary<string, string>();
			foreach (KeyValuePair<string, string> possibleValue in this.PossibleValues)
			{
				strs[possibleValue.Key] = StringLocaliser.LocaliseString(possibleValue.Key, true);
				if (comboBox.Items.IndexOf(strs[possibleValue.Key]) < 0)
				{
					comboBox.Items.Add(strs[possibleValue.Key]);
				}
				if (possibleValue.Key == this.WorkingValue)
				{
					comboBox.SelectedIndex = num;
				}
				num++;
			}
			this.PossibleValues = strs;
		}

		public void ResetToDefault()
		{
			if (this.AttachedControl != null)
			{
				if (this.AttachedControl is ComboBox)
				{
					ComboBox attachedControl = (ComboBox)this.AttachedControl;
					this.Value = this.DefaultValue;
					attachedControl.SelectedItem = StringLocaliser.LocaliseString(this.Value, true);
					return;
				}
				if (this.AttachedControl is TextBox)
				{
					((TextBox)this.AttachedControl).Text = "";
					return;
				}
				if (this.AttachedControl is TrackBar)
				{
					TrackBar trackBar = (TrackBar)this.AttachedControl;
					float single = 1f;
					float.TryParse(this.DefaultValue, out single);
					trackBar.Value = (int)(single * 10f);
				}
			}
		}

		public void RevertValue()
		{
			this.WorkingValue = this.Value;
			if (this.AttachedControl != null)
			{
				if (this.AttachedControl is ComboBox)
				{
					((ComboBox)this.AttachedControl).SelectedItem = this.WorkingValue;
					return;
				}
				if (this.AttachedControl is TextBox)
				{
					((TextBox)this.AttachedControl).Text = this.WorkingValue;
					return;
				}
				if (this.AttachedControl is TrackBar)
				{
					TrackBar attachedControl = (TrackBar)this.AttachedControl;
					float single = 1f;
					float.TryParse(this.WorkingValue, out single);
					attachedControl.Value = (int)(single * 10f);
					return;
				}
				MessageBox.Show("");
			}
		}

		public void SetDefaultValue(string _value)
		{
			this.DefaultValue = _value;
		}

		public void SetSelectedItem(string _item)
		{
			this.Value = _item;
			if (this.AttachedControl is ComboBox)
			{
				((ComboBox)this.AttachedControl).SelectedItem = _item;
			}
		}

		private void tb_TextChanged(object sender, EventArgs e)
		{
			this.WorkingValue = ((TextBox)sender).Text;
		}

		private void trb_ValueChanged(object sender, EventArgs e)
		{
			float value = (float)((TrackBar)sender).Value * 0.1f;
			this.WorkingValue = value.ToString();
		}

		public void UpdateValue()
		{
			this.Value = this.WorkingValue;
		}
	}
}